package com.trade.common.utils;

import org.apache.commons.lang.StringUtils;

/**
 * 正则验证
 * @author zfb
 */
public class RegexpValidate {
    //表名格式校验
    public static final String TABLE_NAME_FORMAT = "^[A-Za-z]+[A-Za-z0-9_]*$";
    //数据库ip格式校验
    public static final String DATABASE_IP_FORMAT = "^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$";
    //系统名称校验
    public static final String DATABASE_CNAME_FORMAT = "[A-Za-z0-9\\u4e00-\\u9fa5]]*$";
    //数据库端口校验
    public static final String DATABASE_PORT_FORMAT = "^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(" +
            "^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)";
    //编码格式校验
    public static final String METHOD_CODE_FORMAT = "^[A-Za-z0-9]+[A-Za-z0-9_]*$";

    //任务触发器表达式
    public static final String CRON_EXPRESSION = "^\\s*($|#|\\w+\\s*=|(\\?|\\*|(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?(?:,(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?)*)\\s+(\\?|\\*|(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?(?:,(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?)*)\\s+(\\?|\\*|(?:[01]?\\d|2[0-3])(?:(?:-|\\/|\\,)(?:[01]?\\d|2[0-3]))?(?:,(?:[01]?\\d|2[0-3])(?:(?:-|\\/|\\,)(?:[01]?\\d|2[0-3]))?)*)\\s+(\\?|\\*|(?:0?[1-9]|[12]\\d|3[01])(?:(?:-|\\/|\\,)(?:0?[1-9]|[12]\\d|3[01]))?(?:,(?:0?[1-9]|[12]\\d|3[01])(?:(?:-|\\/|\\,)(?:0?[1-9]|[12]\\d|3[01]))?)*)\\s+(\\?|\\*|(?:[1-9]|1[012])(?:(?:-|\\/|\\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\\/|\\,)(?:[1-9]|1[012]))?(?:L|W)?)*|\\?|\\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\\s+(\\?|\\*|(?:[0-6])(?:(?:-|\\/|\\,|#)(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\\/|\\,|#)(?:[0-6]))?(?:L)?)*|\\?|\\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)(|\\s)+(\\?|\\*|(?:|\\d{4})(?:(?:-|\\/|\\,)(?:|\\d{4}))?(?:,(?:|\\d{4})(?:(?:-|\\/|\\,)(?:|\\d{4}))?)*))$";

    /**
     * 获取字符串的长度，如果有中文，则每个中文字符计为2位
     *
     * @param value 指定的字符串
     * @return 字符串的长度
     */
    public static int length(String value) {
        int valueLength = 0;
        String chinese = "[^\\x00-\\xff]";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为3，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为2 */
                valueLength += 3;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }

    /**
     * 利用正则表达式将每个中文字符转换为"**"
     * 匹配中文字符的正则表达式： [\u4e00-\u9fa5]
     * 匹配双字节字符(包括汉字在内)：[^\x00-\xff]
     *
     * @param validateStr
     * @return
     */
    public static int getRegExpLength(String validateStr) {
//    	String temp = validateStr.replaceAll("[\u4e00-\u9fa5]", "***");
        String temp = validateStr.replaceAll("[^\\x00-\\xff]", "***");
        return temp.length();
    }

    public static boolean checkCronExpression(String validateStr) {
        return StringUtils.isNotBlank(validateStr) && validateStr.matches(CRON_EXPRESSION);
    }


    public static void main(String[] args) {
        System.out.println(checkCronExpression("0 0/5 14,18 * * ?"));
    }

}
